package com.fzl.spring_chatroom.service;

import com.fzl.spring_chatroom.model.dao.Message;
import com.fzl.spring_chatroom.model.mapper.MessageMapper;
import com.fzl.spring_chatroom.model.response.MessageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class MessageService {
    @Autowired
    private MessageMapper messageMapper;

    public List<MessageResponse> getMessageBySessionId(Integer sessionId) {
        List<MessageResponse> list = messageMapper.getMessageBySessionId(sessionId);
        Collections.reverse(list);
        return list;
    }

    public void addMessage(int sessionId, int fromId, String fromName, String content) {
        Message message = new Message();
        message.setSessionId(sessionId);
        message.setFromId(fromId);
        message.setFromName(fromName);
        message.setContent(content);
        messageMapper.insertMessage(message);
    }
}
